//
//  CZUserModel.swift
//  Weibo
//
//  Created by 董松 on 16/2/27.
//  Copyright © 2016年 董松. All rights reserved.
//

import UIKit

class CZUserModel: NSObject,NSCoding {

    //调用access_token
    var access_token:String?
    //access_token的生命周期
    var expires_in:NSTimeInterval = 0
    //当前授权用户的uid
    var uid:String?
    //在这个类的基础上添加2个属性
    var profile_image_url: String?
    //用户昵称
    var screen_name:String?
    //kvc构造函数
    init(dic: [String:NSObject]) {
        //先调用父类的init
        super.init()
        //kvc赋值
        setValuesForKeysWithDictionary(dic)
    }
    
    //放置崩溃
    override func setValue(value: AnyObject?, forKey key: String) {
        
    }
    
    func save(file: String){
        
        //保存
        //沙盒路径
        let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last!
        //拼接文件路径
        let filePath = (path as NSString).stringByAppendingPathComponent(file)
        
        NSKeyedArchiver.archiveRootObject(self, toFile: filePath)
        
    }
    
    //归档和接档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(profile_image_url, forKey: "profile_image_url")
    }
    
    //接档
    required init?(coder aDecoder: NSCoder) {
        self.access_token = aDecoder.decodeObjectForKey("access_token") as? String
        
        self.expires_in = aDecoder.decodeObjectForKey("expires_in") as! NSTimeInterval
        
        self.uid = aDecoder.decodeObjectForKey("uid") as? String
        self.screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        self.profile_image_url = aDecoder.decodeObjectForKey("profile_image_url") as? String
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
